package com.xin.di.uav.applet.controller;

import cn.hutool.core.util.ObjectUtil;
import com.xin.di.uav.applet.service.OrderService;
import com.xin.di.uav.common.enums.ServicePrefixEnum;
import com.xin.di.uav.common.exception.BusinessException;
import com.xin.di.uav.common.utils.RedisPrefixKey;
import com.xin.di.uav.common.utils.Rs;
import com.xin.di.uav.po.OrderMapper;
import com.xin.di.uav.user.order.po.OrderPO;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author ltq
 */
@RestController
@RequestMapping("/sys/pay/")
public class PayController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrderMapper orderInfoMapper;

    @GetMapping("get-pay-settings")
    @Operation(tags = "获取再次发起支付配置", description = "获取再次发起支付配置")
    public Rs<Map<String, String>> getPaySettings(@RequestParam String orderId) {
        OrderPO orderPO = orderInfoMapper.selectById(orderId);
        if (ObjectUtil.isEmpty(orderPO)) {
            throw new BusinessException("订单不存在");
        }
        Map<String, String> payImgInfo = (Map<String, String>) redisTemplate.opsForValue().get(ServicePrefixEnum.RESCUE + RedisPrefixKey.PAY_SETTING + orderId);
        if (ObjectUtil.isEmpty(payImgInfo)) {
            throw new BusinessException("订单已过期，请重新下单");
        }
        return Rs.success(payImgInfo);
    }
}
